package com.zy.pro.security.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.contact.model.Contact;
import com.zy.pro.contact.web.form.ContactSearchForm;
import com.zy.pro.security.model.Security;
import com.zy.pro.security.service.SecurityService;
import com.zy.pro.security.web.form.SecuritySearchForm;
import com.zy.utils.UploadHelper;
/**
 * 保障金管理控制器
 *
 */
@Controller
@RequestMapping("/control/security/securityController")
public class SecurityController extends BaseController {

	private static final Logger logger = Logger.getLogger(SecurityController.class);
	
	/**
	 * 保障金管理服务类
	 */
	@Resource(name = "securityServiceImpl")
	private SecurityService securityService;
	
	/**
	 * 系统-显示编辑保障金金额管理
	 */
	@RequestMapping("/dispalySysEdit")
	public String dispalySysEdit(Security security, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			if(security != null && security.getSecurityId() != null){
				security = securityService.find(security.getSecurityId());
			}else{
				List<Security> cList = new ArrayList<Security>();
				cList = securityService.findAll(new SecuritySearchForm());
				if(!cList.isEmpty()){
					security = cList.get(0);
				}
			}
			map.addAttribute("operateStatus", "sysEdit");
			map.addAttribute("security", security);
			//TODO 转发页面
			return "/control/system/security/sysEditSecurity";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑保障金金额管理
	 */
	@RequestMapping("/sysEdit")
	public String sysEdit(Security security,ModelMap map, HttpServletRequest request) {
		try {
			
			if(security != null && security.getSecurityId() != null){
				securityService.updateBySelective(security);
			}else{
				//创建时间
				security.setSecurityCreateTime(new Date());
				//该保障金,提供的信誉度为'2'点
				security.setSecurityCredibility("2.00");
				//该保障金的等级为1级(终身制)
				security.setSecurityType(1);
				securityService.save(security);
			}
			return dispalySysEdit(security, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 列出保障金管理
	 */
	@RequestMapping("/list")
	public String list(SecuritySearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<Security> pageView = new PageView<Security>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(securityService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加保障金管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Security security, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加保障金管理
	 */
	@RequestMapping("/add")
	public String add(Security security, ModelMap map) {
		try {
			//TODO 设置security  eg:主键信息
			
			securityService.save(security);
			return list(new SecuritySearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑保障金管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(SecuritySearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Security security = securityService.find(form.getId());
			//eg:map.addAttribute("entity", security);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑保障金管理
	 */
	@RequestMapping("/edit")
	public String edit(Security Security, ModelMap map) {
		try {
			//TODO:处理保障金管理
			
			
			securityService.updateBySelective(Security);
			return list(new SecuritySearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个保障金管理
	 */
	@RequestMapping("/deletesecurity")
	public String delete(SecuritySearchForm form, ModelMap map) {
		try {
			//TODO  eg:securityService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除保障金管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(SecuritySearchForm form, ModelMap map) {
		try {
//			TODO  eg:securityService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
